//
//  icomTableController.swift
//  account
//
//  Created by Qingke on 16/4/25.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

protocol changeImageProtocol : class{
    func changeImage( UIControl: icomTableController, imageName : String )
}

class icomTableController: UITableViewController {
    
    var iconNames = ["生日","工作","娱乐","其他"]
    
    weak var delegate: changeImageProtocol?
    var item: CustomerLtem?
    
    var dataModel: DataModel!

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "选择事件类型"
        let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
        dataModel = appDel.dataModel

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return iconNames.count + dataModel.items.count  //所有的cell 的总数
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("iconCell", forIndexPath: indexPath)
        
        //通过对cell 的甄别来实现返回两个不同类型的cell目的
        if !dataModel.items.isEmpty && indexPath.row >= iconNames.count   {
            //print(items.count)
            let item =  dataModel.items[indexPath.row - iconNames.count]
            cell.imageView?.image = item.image
            cell.textLabel?.text = item.itemName
            
        } else {
            let iconName = iconNames[indexPath.row]
            cell.imageView?.image = UIImage(named: iconName)
            cell.textLabel?.text = iconName
        }
        
        return cell
    }
    
    //点击跳页
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        navigationController?.popViewControllerAnimated(true)
        let iconName: String
        if indexPath.row < iconNames.count {
            iconName = iconNames[indexPath.row]
        } else {
            iconName =  dataModel.items[indexPath.row - iconNames.count].itemName ?? "这个项目没有名字"
        }
        
        delegate?.changeImage(self, imageName: iconName)
        //这里传过去只有一个名字。不传图片了，以后实现这个功能
        dataModel.saveItems()
    }
    
    
    
    //MARK: -IBAcion
    //这里通过没有连线的方式，实现了页面间的数值传递。exit 然后自然出现了那些没有绑定的@IBAction
    //通过判断和转换，赋值。
    @IBAction  func unwindForSegues(segue: UIStoryboardSegue ) {
    
        if let targetVC = segue.sourceViewController as? customerItemViewController , let item1 = targetVC.item {
            item = item1
            print(item1.itemName)
             dataModel.items.append(item1) //添加对象
            print( dataModel.items.count)
            let newIndex = NSIndexPath( forRow: (iconNames.count +  dataModel.items.count) - 1 , inSection: 0 )
            
            tableView.insertRowsAtIndexPaths([newIndex], withRowAnimation: .Fade)
        }else{
            print("没有")
        }
        
    }
    
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }


    
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            iconNames.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    
    

}


